package com.ngc.mall.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.BannerItemDomain;
import com.ngc.mall.param.BannerItemParam;
import com.ngc.mall.service.IBannerItemService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * 
 * @author: chenxiaolin 
 * @date 2023/7/13 18:28
**/
@RestController
@RequestMapping("/banner-item")
public class BannerItemController {

    @Resource
    private IBannerItemService iBannerItemService;

    /**
     * 根据bannerid获取bannerItem列表
     */
    @GetMapping("/list/{bannerId}")
    public ResponseResult<List<BannerItemDomain>> selectAllBannerItem(@PathVariable Long bannerId){
        return ResponseResult.success(iBannerItemService.list(Wrappers.<BannerItemDomain>lambdaQuery().eq(BannerItemDomain::getBannerId,bannerId)));
    }


    /**
     * 创建BannerItemm
     * @return
     */
    @PostMapping("")
    public ResponseResult<Long> createBannerItem(@RequestBody BannerItemParam param){
        BannerItemDomain domain=BannerItemDomain.builder().bannerId(param.getBannerId()).img(param.getImg()).name(param.getName()).sort(param.getSort()).type(param.getType()).path(param.getPath()).build();
        return ResponseResult.success(iBannerItemService.createBannerItem(domain));
    }

    /**
     * 编辑BannerItem
     */
    @PutMapping("/{id}")
    public ResponseResult<Boolean> updateBannerItemById(@PathVariable Long id,@RequestBody BannerItemParam param){
        Wrapper wrapper=Wrappers.<BannerItemDomain>lambdaUpdate().eq(BannerItemDomain::getId,id).set(BannerItemDomain::getImg,param.getImg()).set(BannerItemDomain::getName,param.getName()).set(BannerItemDomain::getPath,param.getPath()).set(BannerItemDomain::getSort,param.getSort()).set(BannerItemDomain::getType,param.getType());
        return ResponseResult.auto(iBannerItemService.update(wrapper));
    }

    /**
     * 删除BannerItem
     */
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> deleteBannerItemById(@PathVariable Long id){
        return ResponseResult.auto(iBannerItemService.removeById(id));
    }
}
